package com.webside.points.controller;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.webside.points.model.SystemUserEntity;
import com.webside.points.service.SystemUserService;



@Controller
@Scope("prototype")
@RequestMapping("/systemUser/")
public class SystemUserController {

	@Autowired
	SystemUserService systemUserService;
	
	@RequestMapping("login")
	public String login(SystemUserEntity systemUserEntity,Model model,HttpServletResponse response,HttpServletRequest request){
		System.out.println("进入系统登录：>>>"+systemUserEntity);
		
		SystemUserEntity entity = systemUserService.loginCheck(systemUserEntity);
		
		if(entity!=null){
			HttpSession session = request.getSession();
			//设置session为10十分钟不操作失效
			session.setMaxInactiveInterval(600); 
			session.setAttribute("userName", entity.getUserName());
			return "redirect:../serve/index.jsp";
		}
		return "redirect:../serve/login.jsp";
	}
	
	@RequestMapping("findAll")
	public String findAll(Model model){
		model.addAttribute("systemUserList", systemUserService.findAll());
		return "serve/systemUser";
	}
	
	@RequestMapping("insert")
	public String insert(SystemUserEntity systemUserEntity){
		
		System.out.println("进入系统用户增加：>>>"+systemUserEntity);
		
		systemUserEntity.setCreateTime(new Date(System.currentTimeMillis()));
		systemUserEntity.setStatus("1");
		systemUserService.insert(systemUserEntity);
		
		return "redirect:findAll";
	}
	
	@RequestMapping("delete")
	public String delete(int id){
		
		System.out.println("进入系统用户删除");
		
		systemUserService.delete(id);
		return "redirect:findAll";
	}
	
	
	@RequestMapping("findById")
	public String findById(int id,Model model){
		model.addAttribute("systemUser", systemUserService.findById(id));
		return "serve/systemUserForm";
	}
	
	@RequestMapping("update")
	public String update(SystemUserEntity systemUserEntity){
		
		System.out.println("进入系统用户修改：>>>"+systemUserEntity);
		
		systemUserEntity.setCreateTime(new Date(System.currentTimeMillis()));
		systemUserService.update(systemUserEntity);
		
		return "redirect:findAll";
	}
	
	
	
}
